package com.icexxx.mapper_gen;

import java.util.List;
import java.util.StringJoiner;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;

public class XmlGen {

    public static String gen(Func func, String packageName) {
        String name = func.getName();
        String comment = func.getComment();
        List<Param> paramList = func.getParamList();
        Param returnParam = func.getReturnParam();
        String tageName = tageName(name);
        CodeGen gen = CodeGen.gen(tageName);
        String nl = "\r\n";
        CodeContext.setPackageName(packageName);
        String content = gen.genXml(name, comment, paramList, returnParam, nl);
        return content;
    }

    private static String tageName(String methodName) {
        if (methodName == null || "".equals(methodName)) {
            return "select";
        }
        if (methodName.startsWith("select")) {
            return "select";
        }
        if (methodName.startsWith("find")) {
            return "select";
        }
        if (methodName.startsWith("query")) {
            return "select";
        }
        if (methodName.startsWith("update")) {
            return "update";
        }
        if (methodName.startsWith("modify")) {
            return "update";
        }
        if (methodName.startsWith("insert")) {
            return "insert";
        }
        if (methodName.startsWith("save")) {
            return "insert";
        }
        if (methodName.startsWith("delete")) {
            return "delete";
        }
        if (methodName.startsWith("del")) {
            return "delete";
        }
        return "select";
    }

    public static String genericity(List<Param> paramList) {
        for (Param param : paramList) {
            String genericity = param.getGenericity();
            if (genericity != null && !"".equals(genericity)) {
                return genericity;
            }
        }
        return null;
    }

    public static String genericity(Param returnParam) {
        String genericity = returnParam.getGenericity();
        if (StrUtil.isBlank(genericity)) {
            genericity = returnParam.getType();
        }
        return genericity;
    }

    public static String pojoName(String packageName, String genericity) {
        genericity = simpleType(genericity);
        String baseType = CodeGen.baseType(genericity);
        if (baseType != null) {
            return baseType;
        }
        StringBuilder sb = new StringBuilder();
        if (StrUtil.isNotBlank(packageName)) {
            sb.append(packageName);
            sb.append(".");
        }
        if (StrUtil.isNotBlank(genericity)) {
            if (genericity.contains(",")) {
                genericity = StrUtil.subAfter(genericity, ",", true);
            }
            sb.append(genericity);
        }
        return sb.toString();
    }

    public static String genericityReturnResultMap(String genericity) {
        genericity = simpleType(genericity);
        return StrUtil.lowerFirst(genericity) + "ResultMap";
    }

    public static String comment(String comment) {
        return StrUtil.wrap(comment, "<!-- ", " -->");
    }

    public static String paramWrap(String paramName) {
        return StrUtil.wrap(paramName, "#{", "}");
    }

    public static String mainCode(String padding, List<String> paramWrapList) {
        StringJoiner joiner = new StringJoiner(", ", padding, "");
        for (String string : paramWrapList) {
            joiner.add(string);
        }
        return joiner.toString();
    }

    public static String simpleType(String type) {
        if (type.contains(",")) {
            type = StrUtil.subAfter(type, ",", true);
        }
        return type;
    }

    public static String simpleType(List<Param> paramList) {
        if (CollUtil.isEmpty(paramList)) {
            return null;
        }
        for (Param param : paramList) {
            String genericity = param.getGenericity();
            if (StrUtil.isBlank(genericity)) {
                String type = param.getType();
                if (StrUtil.isNotBlank(type)) {
                    return type;
                }
            }
        }
        return null;
    }
}
